package com.zoe.onelink.logging.biz.provider.impl;

import com.zoe.onelink.logging.biz.provider.IBizLogOperatorProvider;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-11-26
 */
public class DefaultBizLogOperatorProvider implements IBizLogOperatorProvider {

    public static final String OPERATOR_HEADER_NAME = "operator";

    @Override
    public String getOperatorCode() {
        HttpServletRequest request = this.getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader(OPERATOR_HEADER_NAME);
    }

    @Override
    public String getOperatorName() {
        return null;
    }


    /**
     * 获取当前请求对象
     */
    private HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        return ((ServletRequestAttributes) requestAttributes).getRequest();
    }


}
